package com.ejlchina.tool.singleton

import android.util.Log
import com.ejlchina.tool.util.LOG_TAG
import com.ejlchina.tool.util.bytearray.hex2ByteArray
import com.ejlchina.tool.util.bytearray.readUInt32LE
import com.ejlchina.tool.util.bytearray.toHexString
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec


object AES_128ECB {
    private const val ALGORITHM = "AES"
    const val ALGORITHM_PATTERN_CBC = "AES/CBC/PKCS7Padding"
    private val keyArray = arrayListOf(
        "0000", "1021", "2042" ,"3063", "4084", "50A5", "60C6", "70E7",
        "8108", "9129", "A14A", "B16B", "C18C", "D1AD", "E1CE", "F1EF",
        "1231", "0210", "3273", "2252", "52B5", "4294", "72F7", "62D6",
        "9339", "8318", "B37B", "A35A", "D3BD", "C39C", "F3FF", "E3DE",
        "2462", "3443", "0420", "1401", "64E6", "74C7", "44A4", "5485",
        "A56A", "B54B", "8528", "9509", "E5EE", "F5CF", "C5AC", "D58D",
        "3653", "2672", "1611", "0630", "76D7", "66F6", "5695", "46B4",
        "B75B", "A77A", "9719", "8738", "F7DF", "E7FE", "D79D", "C7BC")

    fun encrypt(mac: String, data: ByteArray,isGetToken: Boolean): ByteArray {
        val key = generateKeyForAES_CBC(mac,isGetToken)
        Log.i(LOG_TAG,"加密生成的密钥为-${key.toHexString(true)}")
        val ivParameterSpec = IvParameterSpec(key)
        val sKeySpec = SecretKeySpec(key, ALGORITHM)
        val cipher = Cipher.getInstance(ALGORITHM_PATTERN_CBC)
        cipher.init(Cipher.ENCRYPT_MODE, sKeySpec,ivParameterSpec)
        return cipher.doFinal(data)
    }

    fun decrypt(mac: String, data: ByteArray): ByteArray {
        val key = generateKeyForAES_CBC(mac,false)
        Log.i(LOG_TAG,"解密生成的密钥为-${key.toHexString(true)}")
        val ivParameterSpec = IvParameterSpec(key)
        val sKeySpec = SecretKeySpec(key, ALGORITHM)
        val cipher = Cipher.getInstance(ALGORITHM_PATTERN_CBC)
        cipher.init(Cipher.DECRYPT_MODE, sKeySpec,ivParameterSpec)
        return cipher.doFinal(data)
    }

    fun generateKeyForAES_CBC(mac: String,isGetToken: Boolean): ByteArray{
        fun generateKeyStepOne(mac: String): String{
            val stepOneResult = StringBuffer()
            mac.split(":").forEach { splitString ->
                stepOneResult.append(keyArray[splitString.toInt(16) % 64])
            }
            return stepOneResult.toString()
        }
        val stepTwoResult = StringBuffer()
        if (isGetToken){
            SharedPreferenceUtil.putString(mac,"00000000")
        }
        val tokenString = SharedPreferenceUtil.getString(mac,"00000000")
        Log.i(LOG_TAG,"获取到的Token为:$tokenString")
        val remainder = (tokenString.hex2ByteArray().readUInt32LE() % 16).toInt()
        Log.i(LOG_TAG,"加密deKey:token:$remainder")
        if (remainder < 13){
            val startString = generateKeyStepOne(mac).substring(0, remainder * 2)
            val endString = tokenString + generateKeyStepOne(mac).substring(remainder * 2)
            stepTwoResult.append(startString)
            stepTwoResult.append(endString)
        } else {
            val tokenCutIndex = tokenString.length - (remainder - 12) * 2
            val startString = tokenString.substring(tokenCutIndex)
            val startBegin = generateKeyStepOne(mac).substring(0,2)
            val tokenEndString = generateKeyStepOne(mac).substring(2)
            val endString = tokenEndString + tokenString.substring(0,tokenCutIndex)
            stepTwoResult.append(startBegin)
            stepTwoResult.append(startString)
            stepTwoResult.append(endString)
            Log.i(LOG_TAG,"加密deKey:token:$tokenString---${stepTwoResult.toString()}")
        }
        return stepTwoResult.toString().hex2ByteArray()
    }
}